'Imports TInput = System.String
'Imports TOutput = System.String

'<Pipeline.ContentProcessorAttribute(DisplayName:="ContentPipelineExtension.ContentProcessor1")>
'Public Class ContentProcessor1
'    Inherits Pipeline.ContentProcessor(Of TInput, TOutput)

'    Public Overrides Function Process(ByVal input As TInput, ByVal context As Pipeline.ContentProcessorContext) As TInput
'        ' TODO: process the input object, and return the modified data.
'        Throw New NotImplementedException
'    End Function
'End Class


Imports Microsoft.Xna.Framework.Content.Pipeline.Processors
Imports Microsoft.Xna.Framework.Content.Pipeline.Graphics
Imports TInput = Microsoft.Xna.Framework.Content.Pipeline.Graphics.NodeContent
Imports TOutput = Microsoft.Xna.Framework.Content.Pipeline.ContentProcessorContext


Namespace TrianglePickingPipeline 'ContentProcessor
    <Pipeline.ContentProcessorAttribute(DisplayName:="ContentPipelineExtension.TrianglePickingPipeline")>
    Public Class TrianglePickingPipeline

        Inherits Microsoft.Xna.Framework.Content.Pipeline.Processors.ModelProcessor
        Private vertices As New List(Of Vector3)()

        Public Overrides Function Process(ByVal input As NodeContent, ByVal context As Microsoft.Xna.Framework.Content.Pipeline.ContentProcessorContext) As ModelContent
            Dim model As ModelContent = MyBase.Process(input, context)
            FindVertices(input)
            Dim tagData As New Dictionary(Of String, Object)()
            model.Tag = tagData
            tagData.Add("Vertices", vertices.ToArray())
            tagData.Add("BoundingSphere", BoundingSphere.CreateFromPoints(vertices))
            Return model
        End Function

        Private Sub FindVertices(node As NodeContent)
            Dim mesh As MeshContent = TryCast(node, MeshContent)
            If mesh IsNot Nothing Then
                Dim absoluteTransform As Matrix = mesh.AbsoluteTransform
                For Each geometry As GeometryContent In mesh.Geometry
                    For Each index As Integer In geometry.Indices
                        Dim vertex As Vector3 = geometry.Vertices.Positions(index)
                        vertex = Vector3.Transform(vertex, absoluteTransform)
                        vertices.Add(vertex)
                    Next
                Next
            End If
            For Each child As NodeContent In node.Children
                FindVertices(child)
            Next
        End Sub
    End Class
End Namespace